package de.htw.noise;

import static java.lang.Math.*;

/**
 * A Set of Interpolation methods
 * 
 * @author m.kandora
 *
 */
public class Interpolation2D {
	
	/**
	 * linear interpolation.  Interpolates between two function values
	 * y0 and y1. x is the amount of interpolation, that is a limit
	 * of [0,1], where 0 is equal to y0 and 1 is equal to y1
	 * 
	 * @param µ the amount of interpolation
	 * @param params two points y0 and y1
	 * @return the interpolated point
	 */
	public double linear(double µ, double y0, double y1) {
		if(µ < 0 || µ > 1) throw new RuntimeException("illegal µ value");
		return y0 * ( 1 - µ) + y1 * µ;
	}
	
	/**
	 * Cosine interpolation. Interpolates between two function values
	 * y0 and y1. x is the amount of interpolation, that is a limit
	 * of [0,1], where 0 is equal to y0 and 1 is equal to y1
	 * 
	 * @param µ the amount of interpolation. 
	 * @param y0 the first function value
	 * @param y1 the second function value
	 * @return
	 */
	public double cosine(double µ, double y0, double y1) {
		if(µ < 0 || µ > 1) throw new RuntimeException("illegal µ value");
		double ϕ = (1.0 - cos(µ * PI)) / 2.0;
		return y0 * (1.0 - ϕ) + y1 * ϕ;
	}
	
	/**
	 * Cubic interpolation. Provides a better interpolation between 
	 * function values but at a cost of speed. This interpolation functions
	 * takes 4 function values instead of 2. 
	 * 
	 * @param µ the amount of interpolation
	 * @param a the value <b><i>before</i></b> y0
	 * @param b y0
	 * @param c	y1
	 * @param d the value <b><i>after</i></b> y1
	 * @return the interpolated value
	 */
	public double cubic(double µ, double a, double b, double c, double d) {
		if(µ < 0 || µ > 1) throw new RuntimeException("illegal µ value");
		double a0 = d - c - a + b;
		double a1 = a - b - a0;
		double a2 = c - a;
		double a3 = b;
		double µ2 = µ * µ;
		double µ3 = µ2 * µ;
		return a0 * µ3 + a1 * µ2 + a2 * µ + a3;
	}
}
